package gorails.com;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import android.text.format.Time;
import android.util.Log;



public class Trip {
	private static Trip instance = null;
	private String line = null;
	private String origin = null;
	private String destination = null;
	private static double fare = 0.0;
	private static int numOfStops = 0;
	private int lineposition = 0;
	private int stationPosition = 0;
	private int destPosition = 0;
	private static boolean isExpress = false;
	private static Time duration ;
	private boolean departOrArrive = false;
	private static Date createdDate = null;
	private int day = 0; //assume to be error free
	private int month = 0; //assume to be error free
	private int year = 0; //assume to be error free
	private int minute = 0; //assume to be error free
	private int hour = 0; //assume to be error free
	private int DAY_OF_WEEK = 0;
	private int line_size = 0;
	private String SQL_line = null;
	private String SQL_station = null;
	private boolean isInFuture = false;
	protected Trip(){
		Calendar calendar = new GregorianCalendar();
	    calendar.setTime(new Date());
	    createdDate = new Date();
		day = calendar.get(Calendar.DAY_OF_MONTH);
		month = calendar.get(Calendar.MONTH);
		year = calendar.get(Calendar.YEAR);
		DAY_OF_WEEK = calendar.get(Calendar.DAY_OF_WEEK);
		minute = createdDate.getMinutes();
		hour = createdDate.getHours();
	}
	public static Trip getInstance(){
		if(instance==null){
			instance = new Trip();
		}
		return instance;
	}
	public static void reset(){
		instance=null;
		System.gc();
	}
	public void isInFuture(boolean val){
		isInFuture = val;
	}
	public boolean getIsInFuture(){
		return isInFuture;
	}
	public void setLineSize(int size){
		line_size = size;
	}
	public int getLineSize(){
		return line_size;
	}
	public void setLinePosition(int pos){
		this.lineposition = pos;
	}
	public void setStationPosition(int pos){
		this.stationPosition = pos;
	}
	public void setArriveOrDepart(boolean value){
		//true is arrive by; false is depart by
		this.departOrArrive = value;
	}
	public void setTime(int hour,int minute){
		this.hour = hour;
		this.minute = minute;
		
	}
	public void setDate(int day,int month,int year){
		this.day = day;
		this.month = month;
		this.year = year;
		Calendar calendar = new GregorianCalendar();
	    calendar.setTime(new Date(year,month,day));
	    this.DAY_OF_WEEK = calendar.get(Calendar.DAY_OF_WEEK);
	}
	public String getMinuteS(){
		return pad(minute);
	}
	public int getDayOfWeek(){
		return this.DAY_OF_WEEK;
	}
	public void setLine(String line){
		this.line = line;
	}
	public void setOrigin(String origin){
		this.origin = origin;
	}
	public void setDestination(String destination){
		this.destination = destination;
	}
	public double getFare(){
		return fare;
	}
	public int getNumOfStops(){
		return numOfStops;
	}
	public boolean isExpress(){
		return isExpress;
	}
	public int getLinePosition(){
		return this.lineposition;
	}
	public int getStationPosition(){
		return this.stationPosition;
	}
	public boolean getArriveOrDepart(){
		//true is arrive by; false is depart by
		return this.departOrArrive;
	}
	public Time getDuration(){
		return duration;
	}
	public String getDepartTime(){
		//note this is weird format for the sql query pattern search
		int hourT = this.hour;
		if(hourT>12){
			//it's pm
			hourT-=12;
			return ""+hourT+":"+pad(minute)+"p";			
		}
		else if (hourT==12){
			return ""+hourT+":"+pad(minute)+"p";
		}
		else if(hourT==0){
			//its midnight
			//this is going to be tough because you need to check for 11p and 12a
			hourT+=12;
			return ""+hourT+":"+pad(minute)+"a";
			
		}
		else{
			//its am
			return ""+hourT+":"+pad(minute)+"a";
		
		}
		
	}
	public String getLine(){
		return line;
	}
	public String getOrigin(){
		return origin;
	}
	public String getDestination(){
		return destination;
	}
	public int getMinute(){
		return minute;
	}
	public int getHour(){
		return hour;
	}
	public int getYear(){
		return year;
	}
	public int getMonth(){
		return month;
	}
	public int getDay(){
		return day;
	}
	private static String pad(int c) {
        if (c >= 10)
            return String.valueOf(c);
        else
            return "0" + String.valueOf(c);
	}
	public String getDateTimeString(){
		int htemp = hour;
		String amPm = "am";
		String departArrive = "Depart at ";
		if(departOrArrive){
			departArrive = "Arrive by ";
		}
		
		if(htemp > 12){
			htemp -= 12;
			amPm="pm";
			return departArrive+htemp+":"+pad(minute)+amPm+" at "+(month+1)+"/"+day+"/"+year;
		}
		else if (htemp==12){
			amPm="pm";
			return departArrive+htemp+":"+pad(minute)+amPm+" at "+(month+1)+"/"+day+"/"+year;
		}
		else if(htemp==0){
			htemp+=12;
			amPm="am";
			return departArrive+htemp+":"+pad(minute)+amPm+" at "+(month+1)+"/"+day+"/"+year;
		}
		else{
			return departArrive+htemp+":"+pad(minute)+amPm+" at "+(month+1)+"/"+day+"/"+year;
		}
		
	}
	public void setDestinationPos(int pos){
		destPosition = pos;
	}
	public int getDestinationPos(){
		return destPosition;
	}
	public void setSQLLine(String line){
		SQL_line = line;
	}
	public String getSQLLine(){
		return SQL_line;
	}
	public void setSQLStation(String station){
		SQL_station = line;
	}
	public String getSQLStation(){
		return SQL_station;
	}
}
